<?php


namespace app\common\model;

use app\common\model\User;
use EasyWeChat\Foundation\Application;
use addons\wechat\library\Config as ConfigService;
use think\Model;

class OrderChanges extends Model
{
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'old_time_text',
        'new_time_text',
    ];
    public function setOldTimeAttr($value)
    {
        return $value && is_numeric($value) ? $value : strtotime($value);
    }

    public function setNewTimeAttr($value)
    {
        return $value && is_numeric($value) ? $value : strtotime($value);
    }

    public function getOldTimeTextAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['old_time']) ? $data['old_time'] : '');
        return $value && is_numeric($value) ? date('Y-m-d',$value) : $value;
    }
    public function getNewTimeTextAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['new_time']) ? $data['new_time'] : '');
        return $value && is_numeric($value) ? date('Y-m-d',$value) : $value;
    }
    public function orders()
    {
        return $this->belongsTo('Order','order_id')->setEagerlyType(0);
    }

    public function scenic()
    {
        return $this->belongsTo('Scenic','scenic_id')->setEagerlyType(0);
    }

    public function change($status,$order_id,$goods_id,$date)
    {

        $order = Order::get($order_id);
        $orderLogModel = new OrderLog();
        $dayModel = new DayInventory(); //库存
        if ($status == 1){

            //同意
            $order->save([
                'rebook' => 1,
                'play_time'          => strtotime($date),
                'play_time_notes'    => $date, //游玩时间备注
            ]);
            $orderLogModel->createLog($order_id, OrderLog::ORDER_CHANG, '订单改签');
            //公众号推送订单通知
                $user = User::get($order['user_id']);
                if ($user['openid']) {
                     $app = new Application(ConfigService::load());   
                      //发送模板消息
                     $app->notice->send([
                                          'touser' => $user['openid'],
                                          'template_id' => 'spxax86Bd3C902GpS5oe7CwzG3u_xCneXdMP0Xwuczw',
                                          'url' => 'http://www.store.liuyiqinzi.com/orderinfo/'.$order['id'].'/2',
                                          'miniprogram'=>['appid'=>'wxab379d255ecd6df9',
                                                          'pagepath'=>'pages/index/Splash?page=orderdetail&orderId='.$order['id'].'&orderType=2'
                                                      ],
                                          'data' => [
                                              'first' => ['您好，您的改签订单已通过审核','#52627C'],
                                              'keyword1' => [$order['id'],'#DB9728'],
                                              'keyword2' => [$order['order_phone'],'#52627C'],
                                              'keyword3' => ['审核通过','#52627C'],
                                              'remark' => ['请点击查看订单详情','#52627C'],
                                          ],
                                      ]);
                    }

        }else if ($status == 2) {
            //公众号推送订单通知
                $user = User::get($order['user_id']);
                if ($user['openid']) {
                     $app = new Application(ConfigService::load());   
                      //发送模板消息
                     $app->notice->send([
                                          'touser' => $user['openid'],
                                          'template_id' => '598j4HaQiorne_BL3LfK8n6tLpBM8JsopRR7qDk-8qw',
                                          'url' => 'http://www.store.liuyiqinzi.com/orderinfo/'.$order['id'].'/2',
                                          'miniprogram'=>['appid'=>'wxab379d255ecd6df9',
                                                          'pagepath'=>'pages/index/Splash?page=orderdetail&orderId='.$order['id'].'&orderType=2'
                                                      ],
                                          'data' => [
                                              'first' => ['您好，您的改签订单没有通过审核','#52627C'],
                                              'keyword1' => [$order['id'],'#DB9728'],
                                              'keyword2' => [$order['order_phone'],'#52627C'],
                                              'keyword3' => ['审核未通过','#52627C'],
                                              'remark' => ['请点击查看订单详情','#52627C'],
                                          ],
                                      ]);
                    }
        }else{
            //拒绝
            $order->save([
                'rebook' => 1,
            ]);  
        }
    }

}